<template>
  <div>
    <el-tree ref="tree" :data="list" :props="{label:'name'}" default-expand-all check-strictly show-checkbox node-key="id" />
    <div style="text-align: right;">
      <el-button>取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getRolesDetail, assignPrem } from '@/api/settings'
import { getPermissionList } from '@/api/permisson'
import { transTreeList } from '@/utils/index'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPrem({ id: this.roleId, permIds: permIds })
        console.log(res, 'assignPrem')
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        const perIds = await getRolesDetail(this.roleId)
        console.log(perIds, 'getRolesDetail')
        console.log('getPermissionList', res)
        this.$refs.tree.setCheckedKeys(perIds.data.data.permIds)
        this.list = transTreeList(res.data.data)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>
